package at.jku.se.fastsell.db4o.transactions;

import java.util.LinkedList;
import java.util.List;

import at.jku.se.fastsell.data.User;
import at.jku.se.fastsell.db4o.DatabaseException;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

public class GetAllUsersTransaction extends Transaction<List<User>> {
	
	@Override
	public List<User> run() throws DatabaseException {
		List<User> users = new LinkedList<User>();
		
//		ObjectSet<User> result = db.queryByExample(User.class);
//		users.addAll(result);
		
//		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
//	    Key key = KeyFactory.createKey("User", "Fastsell");
	    // Run an ancestor query to ensure we see the most up-to-date
	    // view of the Greetings belonging to the selected Guestbook.
	    Query query = new Query("User", key);
	    Query query2 = new Query("User");
//	    List<Entity> userEntities = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
	    List<Entity> userEntities = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
	    
	    for (Entity userEntity : userEntities) {
	    	User user = new User();
	    	user.setName((String) userEntity.getProperty("name"));
	    	user.setPassword((String) userEntity.getProperty("password"));
	    	users.add(user);
	    }
	    
	    return users;
	}

}
